import re

# 导入ocr模块
import ddddocr

# 导入字体解析模块
from fontTools.ttLib import TTFont

# 导入io模块
import io

# 导入PIL
from PIL import Image, ImageDraw, ImageFont

# 导入请求模块
import requests


# 请求自定义字体地址, 保存自定义字体
def get_font():
    font_path = "./static/font/font"
    font_headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"}
    font_url = "https://www.shixiseng.com/interns/iconfonts/file"
    response = requests.get(font_url, headers=font_headers)
    with open(font_path, "wb") as f:
        f.write(response.content)
    return font_path

# 获取字符映射表
def get_font_maps():
    # 创建空的字符映射表
    font_maps = {}
    # 创建ocr
    ocr = ddddocr.DdddOcr()
    # 获取字体路径
    font_path = get_font()
    # 解析字体
    font = TTFont(font_path)
    # 拿取编码和对应的字符
    for code, name in font.getBestCmap().items():
        # 过滤 x 字符
        if code == 120:
            continue
        else:
            # 1. 创建图片背景
            img = Image.new("RGB", (25, 25), (255, 255, 255))
            # 2. 设置字体
            img_font = ImageFont.truetype(font_path, 20)
            # 3. 创建画笔, 绘画字符
            draw = ImageDraw.Draw(img)
            draw.text((0,0), text=chr(code), fill=(0,0,0), font=img_font)
            # 4. 保存图片
            img.save(f"./static/font/imgs/{code}.png", "png")
            # 创建内存流
            img_bytes = io.BytesIO()
            # 保存图片到内存流, 避免磁盘I/O, 速度快
            img.save(img_bytes, "PNG")
            # 取出图片二进制
            img_byte = img_bytes.getvalue()
            # ocr识别图片
            result = ocr.classification(img_byte)
            # 添加结果到字符映射集中
            font_maps[hex(code)] = result
    print(font_maps)
    return font_maps

# 用字体映射表解析动态字体
def parse_filed(value, font_maps):
    # 获取字体映射表
    font_maps = font_maps
    # 构造正则表达式
    params = "&#x([0-9a-f]+)"
    # match由sub()方法,  自动创建一个 re.Match对象传入
    def decode_value(match):
        # 选取正则表达式匹配的()的结果
        s = match.group(1).lower()
        # 构造key值
        char = f"0x{s}"
        if font_maps[char]:
            # 返回自定义字符转换成十六进制后对应的标准字符
            return font_maps[char]
        else:
            # 找不到返回原字符
            s = int(s, 16)
            print(f"未找到{char}对应的字符, 返回原始字符{s}")
            return s
    # 找到value中所有满足params的值到替换函数中执行, 把结果集执行+操作
    result = re.sub(params, decode_value, value)
    # 返回正确结果
    return result


# 请求动态字体网页, 获取数据
def get_info():
    font_maps = get_font_maps()
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"}
    urls = [f"https://www.shixiseng.com/app/interns/search/v2?build_time=1763383387320&page={page}&type=intern&keyword=&area=&months=&days=&degree=&official=&enterprise=&salary=-0&publishTime=&sortType=&city=%E6%9D%AD%E5%B7%9E&internExtend=" for page in range(1, 11)]
    for url in urls:
        print(f"开始爬取第{urls.index(url)+1}页", url)
        response = requests.get(url, headers=headers)
        items = response.json()["msg"]["data"]
        for item in items:
            title = item['name']
            url = item['url']
            company = item['cname']
            tags = item["industry"]
            max_money = item['maxsal']
            min_money = item['minsal']
            print(f"工作:{parse_filed(title, font_maps)}, 价格:{parse_filed(min_money, font_maps)}-{parse_filed(max_money, font_maps)}/天, 公司:{company}, 标签:{tags}, 详情地址:{url}")




if __name__ == '__main__':
    # s = parse_filed("&#xe23a业研究实习&#xec2e")
    # print(s)
    get_info()


